/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.component.feedback;

import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.Component;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.feedback.IFeedbackMessageFilter;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

/**
 * Custom Feedback Panel.
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class CustomFeedbackPanel extends FeedbackPanel
{

  private static final long serialVersionUID = 1L;
  /**
   * Levels as strings for debugging.
   */
  private static final Map<Integer, String> levelStrings = new HashMap<Integer, String>();

  static
  {
    levelStrings.put(FeedbackMessage.UNDEFINED, "alert-info");
    levelStrings.put(FeedbackMessage.DEBUG, "alert-info");
    levelStrings.put(FeedbackMessage.INFO, "alert-info");
    levelStrings.put(FeedbackMessage.SUCCESS, "alert-success");
    levelStrings.put(FeedbackMessage.WARNING, "alert-warning");
    levelStrings.put(FeedbackMessage.ERROR, "alert-danger");
    levelStrings.put(FeedbackMessage.FATAL, "alert-danger");
  }

  /**
   * Constructor
   *
   * @param id
   * @param component If this argument is passed, the messages will be displayed only for this component
   */
  public CustomFeedbackPanel(final String id, final Component component)
  {
    this(id);

    setFilter(new ComponentFeedbackMessageFilter(component));
  }

  /**
   * Constructor
   *
   * @param id
   */
  public CustomFeedbackPanel(final String id)
  {
    super(id);
  }

  @Override
  protected String getCSSClass(final FeedbackMessage message)
  {
    return levelStrings.get(message.getLevel());
  }

  /**
   * Custom filter. Enables only messages which belong to the given component
   */
  private class ComponentFeedbackMessageFilter implements IFeedbackMessageFilter
  {
    // The component which will be checked
    final Component component;

    /**
     * Constructor
     * @param component
     */
    public ComponentFeedbackMessageFilter(final Component component)
    {
      this.component = component;
    }

    @Override
    public
    boolean accept(FeedbackMessage message)
    {
      return message.getReporter().equals(component);
    }

  }
}
